/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Алексей
 */

package ru.amse.podolsky.fractalarch;


import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;


public class ArchivIO {

    //Constants
    private final int numberOfDomenParams = 6;
    private final int enterCharCode = 13;
    private final int spaceCharCode = 32;
    private final int zeroCharCode = 48;
    private final int eofCharCode = -1;
    
    private String path;
    
    ArchivIO(String s){
        this.path = s;
    }
    
    public void write(DomenParams[] params,int numberOfDomens, int middleColor, int width, int height, String step) throws IOException {
        PrintWriter output = new PrintWriter((this.path));  
        
        output.print(numberOfDomens);
        output.print(" ");
        output.print(middleColor);
        output.print(" ");
        output.print(width);
        output.print(" ");
        output.print(height);
        output.print(" ");
        output.println(step);
        for(int i =0; i < params.length; ++i){
            output.print(params[i].domenXCoord);
            output.print(" ");
            output.print(params[i].domenYCoord);
            output.print(" ");
            output.print(params[i].sameBlockNumber);
            output.print(" ");
            output.print(params[i].shift);
            output.print(" ");
            output.print(params[i].numberOfConversion);
            output.print(" ");
            output.print(params[i].testDelta);
            //output.print(" ");
            output.println();
        }
        output.close();
    }
    
    public int[][] read() throws IOException {
        FileReader input = new FileReader(this.path);
        
        int symbol = input.read();
        int param = 0;
        while(symbol != this.eofCharCode && symbol != this.spaceCharCode){
            if(param == 0){
               param = symbol - this.zeroCharCode;
            }else{
               param = param * 10 + symbol - this.zeroCharCode;
            }
            symbol = input.read();
        }
        
        int[][] out = new int [param+1][this.numberOfDomenParams];
        out[0][0] = param;
        
        param = 0;
        symbol = input.read();
        for(int i = 1; i < 5; ++i){
            while(symbol != this.eofCharCode && symbol != this.spaceCharCode && symbol != this.enterCharCode){
                if(param == 0){
                    param = symbol - this.zeroCharCode;
                }else{
                    param = param * 10 + symbol - this.zeroCharCode;
                }
                symbol = input.read();
            }
            out[0][i] = param;
            param = 0;
            symbol = input.read();
        }
        symbol = input.read();
                
        int domen  = 1;
        int number = 0;
        int current = 0;
        while(symbol != this.eofCharCode){
            if(symbol == this.enterCharCode){
                ++domen;
                number = 0;
                current = 0;
                symbol = input.read();
            }else{
              if(symbol == this.spaceCharCode){
                  out[domen][number++] = current;
                  current = 0;
              }else{
                  if(current == 0){
                      current = symbol - this.zeroCharCode;
                  }else{
                  current = current * 10 + symbol - this.zeroCharCode;
                  }
              } 
            }
            symbol = input.read();
        }
        
        input.close();
        
        return out;
    }
    
    
    public String archivName(String input){
        int length = input.length();
        char[] pathwrite = new char[length];
        for(int i = 0; i < length - 3; ++i){
            pathwrite[i] = input.charAt(i);
        }
        pathwrite[length-3] = 'f';
        pathwrite[length-2] = 'a';
        pathwrite[length-1] = 'h';
        return pathwrite.toString();
    }
    
}
